import time
from common.name_mapping import *
from dingTalk import *


def pytest_terminal_summary(terminalreporter, exitstatus, config):
    """
    收集测试结果
    """
    failed_list = []
    error_list = []
    total = terminalreporter._numcollected
    passed = len(terminalreporter.stats.get('passed', []))
    failed_data = terminalreporter.stats.get('failed', [])
    error_data = terminalreporter.stats.get('error', [])
    try:
        # 循环名称，从字段取值
        for failed_item in range(len(failed_data)):
            failed_list.append(function_name[failed_data[failed_item].head_line.split(".")[1]])
        for error_item in range(len(error_data)):
            error_list.append(function_name[error_data[error_item].head_line.split(".")[1]])
    except Exception as e:
        print(e)
    duration = time.time() - terminalreporter._sessionstarttime
    content = f"本次执行测试用例共:{total}个,通过:{passed}个,失败:{len(failed_list)}个,错误:{len(error_list)}个,共消耗:{duration:.2f}秒"
    failed = len(failed_list)
    error = len(error_list)
    if failed == 0 and error == 0:
        print(content)
    elif failed != 0 and error != 0:
        print(content + f",\n失败case为:{failed_list},\n错误case为:{error_list}")
    elif failed != 0 and error == 0:
        print(content + f",\n失败case为:{failed_list}")
    else:
        print(content + f",\n错误case为:{error_list}")
